package cn.com.infcn.monitor.web;

import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

public class CounterServletResponseWrapper extends FilterServletResponseWrapper {
	public CounterServletResponseWrapper(HttpServletResponse response) {
		super(response);
		assert response != null;
	}

	public int getDataLength() {
		return getCounterResponseStream() == null ? 0 : getCounterResponseStream().getDataLength();
	}

	@Override
	public void reset() {
		super.reset();
		resetStream();
	}

	@Override
	public void resetBuffer() {
		super.resetBuffer();
		resetStream();
	}

	private void resetStream() {
		if (getCounterResponseStream() != null) {
			getCounterResponseStream().reset();
		}
	}

	private CounterResponseStream getCounterResponseStream() {
		return (CounterResponseStream) getStream();
	}

	@Override
	public ServletOutputStream createOutputStream() throws IOException {
		return new CounterResponseStream((HttpServletResponse) getResponse());
	}
}
